// 这里是日志网络请求接口
import type { AxiosResponse } from 'axios'
import axios, { AxiosDefaultTypes } from '@/utils/axios'
import type { AxiosJournalListTypes } from './types'

export * from './types'

// 存入日志信息
// type => 日志类型
// title => 日志标题
// author => 登录人 (登录日志)
// equipment => 登录设备 (登录日志) 0 => PC端 1 => 手机端(Android)  2 => 手机端(iPhone)
// loginIP => 登录IP地址（登录日志）
// loginPosition => 登录地理位置 (登录日志)
export const journalStore: ({
  type,
  title,
  author,
  equipment,
  loginIP,
  loginPosition
}: {
  type: number
  title?: string
  author?: string
  equipment?: number
  loginIP?: string
  loginPosition?: string
}) => Promise<AxiosResponse<any, any>> = ({
  type,
  title,
  author,
  equipment,
  loginIP,
  loginPosition
}) => {
  return axios.post<AxiosDefaultTypes<null>>('/api/journal/store', {
    type,
    author,
    equipment,
    title,
    loginIP,
    loginPosition
  })
}

// 获取日志信息
/**
 * type: 日志类型  0 => 登录日志  1 => 错误日志 2 => 操作日志
 * limit: 每次请求日志条数
 * skip: 每次请求数据初始值
 */
export const journalListData: (
  type: number,
  limit: number,
  skip: number
) => Promise<AxiosResponse<AxiosDefaultTypes<AxiosJournalListTypes>, any>> = (
  type: number,
  limit: number,
  skip: number
) => {
  return axios.get<AxiosDefaultTypes<AxiosJournalListTypes>>(
    '/api/journal/requset',
    {
      params: {
        type,
        limit,
        skip
      }
    }
  )
}

// 获取用户IP和地址
export const userIPAndPosition: () => Promise<
  AxiosResponse<any, any>
> = async () => {
  return await axios.get('/api/userIP')
}

// 搜索登录日志信息
/**
 * auhtor: 用户ID
 * startTime: 起始时间
 * endTime: 终止时间
 * equipment: 设备型号
 * limit: 数据个数
 * skip: 数据起始值
 */
export const searchloginJournal: (
  author: string,
  startTime: number,
  endTime: number,
  equipment: number,
  limit: number,
  skip: number
) => Promise<AxiosResponse<AxiosDefaultTypes<AxiosJournalListTypes>, any>> = (
  author: string,
  startTime: number,
  endTime: number,
  equipment: number,
  limit: number,
  skip: number
) => {
  return axios.post<AxiosDefaultTypes<AxiosJournalListTypes>>(
    '/api/journal/searchlogin',
    {
      author,
      startTime,
      endTime,
      equipment,
      limit,
      skip
    }
  )
}

// 删除日志
/**
 *
 * @param journalID 日志ID
 * @returns
 */
export const deleteJournal: (
  journalID: string
) => Promise<AxiosResponse<AxiosDefaultTypes<null>, any>> = (
  journalID: string
) => {
  return axios.post<AxiosDefaultTypes<null>>('/api/journal/delete', {
    journalID
  })
}

// 搜索操作日志信息
/**
 *
 * @param operationType 操作类型 0 => 用户更新了个人资料  1 => 用户修改密码  2 => 用户修改头像 3 => 用户修改封面
 * @param startTime 起始时间
 * @param endTime 结束时间
 * @param limit 数据个数
 * @param skip 数据起始值
 */
export const searchoperationJournal: (
  operationType: number,
  startTime: number,
  endTime: number,
  limit: number,
  skip: number
) => Promise<AxiosResponse<AxiosDefaultTypes<AxiosJournalListTypes>, any>> = (
  operationType: number,
  startTime: number,
  endTime: number,
  limit: number,
  skip: number
) => {
  return axios.post<AxiosDefaultTypes<AxiosJournalListTypes>>(
    '/api/journal/searchOperation',
    {
      operationType,
      startTime,
      endTime,
      limit,
      skip
    }
  )
}
